<?php

class sfApplyApplyForm extends sfGuardUserProfileForm
{  
  public function configure()
  {
    parent::configure();
    unset($this['user_id']);
    $this->widgetSchema['username'] = new sfWidgetFormInput();
    $this->validatorSchema['username'] = new sfValidatorString(array('max_length' => 50, 'min_length' => 3),array('required'=>"Обов'язкове поле"));


    $this->setWidget('password', new sfWidgetFormInputPassword(array(), array('maxlength' => 128)));
    $this->setWidget('password2', new sfWidgetFormInputPassword(array(), array('maxlength' => 128)));
    $this->widgetSchema['captcha'] = new sfWidgetCaptchaGD();

    $this->validatorSchema['captcha'] = new sfCaptchaGDValidator(array('length' => 4),array('required'=>"Обов'язкове поле", 'invalid'=>"Невірно введені цифри"));

    // Form Validators
    $this->setValidator('password', new sfValidatorString(array(
      'required' => true,
      'trim' => true,
      'min_length' => 6,
      'max_length' => 128
    ),array('min_length' => 'Занадто короткий пароль. Мінімальна довжина 6 символів.',
        'required' => "Обов'язкове поле"
        )));
        
    $this->setValidator('password2', new sfValidatorString(array(
      'required' => true,
      'trim' => true,
    ) ,array('required' => "Обов'язкове поле")
      ));
    // Form Widgets positions
    $this->widgetSchema->moveField('username', sfWidgetFormSchema::FIRST);
    $this->widgetSchema->moveField('password', sfWidgetFormSchema::AFTER, 'username');
    $this->widgetSchema->moveField('password2', sfWidgetFormSchema::AFTER, 'password');   
    $this->widgetSchema->moveField('first_name', sfWidgetFormSchema::AFTER, 'password2');
    $this->widgetSchema->moveField('last_name', sfWidgetFormSchema::AFTER, 'first_name');
    $this->widgetSchema->moveField('captcha', sfWidgetFormSchema::AFTER, 'last_name');

    $this->validatorSchema['first_name']->setOption('required',true);
    $this->validatorSchema['last_name']->setOption('required',true);
    $this->validatorSchema['first_name']->setMessage('required', "Обов'язкове поле");
    $this->validatorSchema['last_name']->setMessage('required', "Обов'язкове поле");

    $this->widgetSchema['username']->setLabel("Логін");
    $this->widgetSchema['password']->setLabel("Пароль");
    $this->widgetSchema['captcha']->setLabel("Введіть код");
    $this->widgetSchema['password2']->setLabel("Повторити пароль");

    $this->widgetSchema->setNameFormat('sfApplyApply[%s]');
    $this->widgetSchema->setFormFormatterName('list');
    
    $this->mergePostValidator(
       new sfValidatorPropelUnique(
       array('model' => 'sfGuardUser','column' => array('username')),
       array('invalid' => 'Аккаунт з таким логіном вже зареєстрований')));
    $this->mergePostValidator(
        new sfValidatorSchemaCompare('password',sfValidatorSchemaCompare::EQUAL,'password2',
        array(),
        array('invalid' => 'Паролі не співпадають')));

  }
  
  public function doSave($con = null)
  {
    $user = new sfGuardUser();
    $user->setUsername($this->getValue('username'));
    $user->setPassword($this->getValue('password'));
    $user->save();
    $this->userId = $user->getId();
    $user->addPermissionByName('user');
    return parent::doSave($con);
  }
  
  public function updateObject($values = null)
  {
    $object = parent::updateObject($values);
    $object->setUserId($this->userId);
    return $object;
  }
}
